package com.yjt.fresh.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvHistoryDetail;
import com.yjt.fresh.adpater.AdpVpFrgHistoryVideo;
import com.yjt.fresh.api.ApiGetHistoryDetailList;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanHistory;
import com.yjt.fresh.bean.BeanHistoryDetail;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.fragment.FrgVideo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by UPC on 2018/8/14.
 */

public class AtyHistoryDetail extends BaseSimpleActivity {
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.rvAppAtyHistoryDetail)
    RecyclerView mRv;
    @BindView(R.id.vpAppAtyHistoryDetail)
    ViewPager mVp;
    private AdpRvHistoryDetail mAdpRvHistoryDetail;


    @Override
    protected int getContentView() {
        return R.layout.app_aty_history_detail;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

    }

    @Override
    protected void initEvent() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initRv();
        getData();
        initVP((BeanHistory) getIntent().getSerializableExtra(MyConstants.VIDEO_OBJ));
    }

    private void initVP(BeanHistory beanHistory) {
        ArrayList<FrgVideo> frgVideos = new ArrayList<>();
        FrgVideo frgVideo = new FrgVideo();
        Bundle bundle = new Bundle();
        bundle.putSerializable(MyConstants.VIDEO_OBJ, beanHistory);
        frgVideo.setArguments(bundle);
        frgVideos.add(frgVideo);
        AdpVpFrgHistoryVideo adpVpFrgHistoryVideo = new AdpVpFrgHistoryVideo(getSupportFragmentManager(), frgVideos);
        mVp.setAdapter(adpVpFrgHistoryVideo);
    }

    HttpOnNextListener<List<BeanHistoryDetail>> mListener = new HttpOnNextListener<List<BeanHistoryDetail>>() {
        @Override
        public void onNext(List<BeanHistoryDetail> beanHistoryDetails) {
            mAdpRvHistoryDetail.setNewData(beanHistoryDetails);
        }

        @Override
        public void onReLogin() {
            toLogin();
        }

        @Override
        public void failed(String massage) {
            super.failed(massage);
            Toast.makeText(mContext, massage, Toast.LENGTH_SHORT).show();
            finish();
        }
    };

    private void getData() {
        ApiGetHistoryDetailList apiGetHistoryDetailList = new ApiGetHistoryDetailList(mListener, mContext, true, false, false);
        Map<String, Object> map = new HashMap<>();
        map.put("SOURCE_SID", getIntent().getStringExtra(MyConstants.SOURCE_SID));
        apiGetHistoryDetailList.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetHistoryDetailList);
    }

    private void initRv() {
        List<BeanHistoryDetail> list = new ArrayList<>();
        mAdpRvHistoryDetail = new AdpRvHistoryDetail(R.layout.app_recycle_item_express, list);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(manager);
        mRv.setAdapter(mAdpRvHistoryDetail);
    }


    @Override
    protected void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }
}
